package com.yudean.esb.service.impl.erp;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.yudean.esb.dao.erp.ErpIdsInsDangerIntDao;
import com.yudean.esb.domain.erp.ErpIdsInsDangerInt;
import com.yudean.esb.service.ErpIdsInsDangerIntService;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.Iterator;

import static com.yudean.esb.util.XmlUtil.returnInfo;
/**
 * Created by zenghui on 2015-05-20.
 * desc:
 */
@WebService(endpointInterface = "com.yudean.esb.service.ErpIdsInsDangerIntService", serviceName = "ErpIdsInsDangerIntService",targetNamespace = "http://service.esb.yudean.com/")
@Service("erpIdsInsDangerIntServiceImpl")
@Scope("prototype")
public class ErpIdsInsDangerIntServiceImpl implements ErpIdsInsDangerIntService {
    private static final String TAG = "ErpIdsInsDangerIntServiceImpl";
    private static final String SERVICE_NAME = "出险赔付信息";
    private static final String SERVICE_TYPE = "写入数据";
    @Resource
    private ErpIdsInsDangerIntDao erpIdsInsDangerIntDao;



    /**
     *
     * @param dangerIntXML
     * 格式：
     *  <?xml version="1.0" encoding="UTF-8"?>
     *   <service>
     *       <name>出险赔付信息</name>
     *       <type>写入数据</type>
     *       <data>
     *           <ErpIdsInsDangerInt>
     *               <compCode>H</compCode>
     *               <insNumber>dd</insNumber>
     *               <endorSementNum>ddasd</endorSementNum>
     *               <dangerDate>2015-05-20 00:33:09.716 UTC</dangerDate>
     *               <insSubject>dasd</insSubject>
     *               <advanceAmount>22</advanceAmount>
     *               <estimateAmount>22</estimateAmount>
     *               <insReason>dasd</insReason>
     *               <closeType>dasdf</closeType>
     *               <closeResult>dasf</closeResult>
     *               <processStatus>dfasd</processStatus>
     *               <fillingNum>dfasd</fillingNum>
     *               <creationDate>2015-05-20 00:33:09.717 UTC</creationDate>
     *               <createdBy>22</createdBy>
     *               <lastUpdateDate>2015-05-20 00:33:09.717 UTC</lastUpdateDate>
     *               <lastUpdatedBy>22</lastUpdatedBy>
     *               <attribute1>33</attribute1>
     *               <attribute2>33</attribute2>
     *               <attribute3>33</attribute3>
     *               <attribute4>33</attribute4>
     *               <attribute5>33</attribute5>
     *               <attribute6>33</attribute6>
     *               <attribute7>33</attribute7>
     *               <attribute8>33</attribute8>
     *               <attribute9>33</attribute9>
     *               <attribute10>33</attribute10>
     *           </ErpIdsInsDangerInt>
     *           <ErpIdsInsDangerInt>
     *               ...
     *               <createdBy>22</createdBy>
     *                <lastUpdateDate>2015-05-20 00:33:09.717 UTC</lastUpdateDate>
     *               <lastUpdatedBy>22</lastUpdatedBy>
     *                <attribute1>33</attribute1>
     *               ...
     *           </ErpIdsInsDangerInt>
     *           ...
     *       </data>
     *   </service>
     *
     * @return
     */
    @Override
    public String putDangerInt(String dangerIntXML) {
        if (StringUtils.isBlank(dangerIntXML)) {
            return returnInfo(SERVICE_NAME,SERVICE_TYPE,"blank params");
        }
        try {
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("ErpIdsInsDangerInt", ErpIdsInsDangerInt.class);
            Document dom = DocumentHelper.parseText(dangerIntXML);
            Element root = dom.getRootElement();
            Element dataList = root.element("data");
            for (Iterator i = dataList.elementIterator(); i.hasNext();) {
                Element data = (Element) i.next();
                String eleXml = data.asXML();
                ErpIdsInsDangerInt erpIdsInsDangerInt = (ErpIdsInsDangerInt) xstream.fromXML(eleXml);
                erpIdsInsDangerIntDao.insert(erpIdsInsDangerInt);
            }
            return returnInfo(SERVICE_NAME, SERVICE_TYPE,"insert success");
        } catch (Exception e) {
            return returnInfo(SERVICE_NAME, SERVICE_TYPE,"insert error" + e.getMessage());
        }

    }


}
